package com.right.health.app.ui.medical;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.CollectionUtil;
import com.magic.basic.utils.ToastUtil;
import com.magic.basic.widget.refresh.OnLastItemVisibleListener;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.app.App;
import com.right.health.app.bean.SurgeryResponse;
import com.right.health.app.net.SickHistoryApi;
import com.right.health.app.ui.medical.adapter.SurgeryListAdapter;
import com.right.health.app.widget.ProgressLayout;

import java.util.ArrayList;
import java.util.List;

import static com.right.health.app.constants.BroadCastCode.REFRESH_SURGERY;

/*
 * Created by hopeliao on 2019-09-30
 *
 */
public class SurgeryListActivity extends BaseActivity implements OnHttpListener, OnLastItemVisibleListener, SwipeRefreshLayout.OnRefreshListener {

    private static final int HTTP_CODE_REFRESH = 1;
    private static final int HTTP_CODE_LOAD_MORE = 2;

    private SurgeryListAdapter mAdapter;

    private SickHistoryApi mSickHistoryApi;

    private SwipeListRefreshLayout mRefreshView;

    private ProgressLayout mProgressLy;

    private int mPageNo;

    private List<SurgeryResponse.SurgeryInfo> mDataSource = new ArrayList<>();

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.surgery_list);
    }

    @Override
    public void findView() {
        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.refresh_view);
        mRefreshView.setLastItemVisibleListener(this);
        mRefreshView.setOnRefreshListener(this);
        mProgressLy = findViewById(R.id.pl);
        ListView listView = (ListView) findViewById(R.id.listview);
        mAdapter = new SurgeryListAdapter(this);
        listView.setAdapter(mAdapter);
        listView.addHeaderView(LayoutInflater.from(this).inflate(R.layout.surgery_list_header, null));

        findViewById(R.id.ib_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startIntent(SurgeryAddActivity.class);
            }
        });
    }

    @Override
    public void initialize() {
        mSickHistoryApi = new SickHistoryApi();
        mSickHistoryApi.setListener(this);

        onReceiveBroadcast(REFRESH_SURGERY, null);
    }


    @Override
    public void onReceiveBroadcast(int intent, Bundle bundle) {
        super.onReceiveBroadcast(intent, bundle);
        if (intent == REFRESH_SURGERY) {
            mRefreshView.setRefreshing(true);
            onRefresh();
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        mRefreshView.onRefreshComplete();
        if(response.isResponseFail()) {
            ToastUtil.show(this, response.error);
            mProgressLy.showInfo();
        } else {
            if(response.targetData instanceof SurgeryResponse) {
                SurgeryResponse surgeryResponse = response.cast(SurgeryResponse.class);
                if (surgeryResponse.page != null) {
                    mRefreshView.setCanLoadMore(!surgeryResponse.page.lastPage);
                }

                if(request.requestCode == HTTP_CODE_REFRESH) {
                    mDataSource.clear();
                }
               if (!CollectionUtil.isEmpty(surgeryResponse.operationhistorys)) {
                   mDataSource.addAll(surgeryResponse.operationhistorys);
                   mAdapter.setDataSource(mDataSource);
                   mProgressLy.hide();
               }else {
                   mProgressLy.showInfo("暂无手术史");
               }
            }
        }
    }

    @Override
    public void onRefresh() {
        mPageNo = 1;

        mSickHistoryApi.addRequestCode(HTTP_CODE_REFRESH);
        mSickHistoryApi.getSurgerys(App.getInstance().getCurrentMember().id, mPageNo);
    }

    @Override
    public void onLastItemVisible() {
        mPageNo++;
        mSickHistoryApi.addRequestCode(HTTP_CODE_LOAD_MORE);
        mSickHistoryApi.getSurgerys(App.getInstance().getCurrentMember().id, mPageNo);
    }
}
